Skip to content

Commit 17f9900

Browse files
committed
Updating the way crafts were blocked -> More clean + stable
1 parent e8f3c88 commit 17f9900

11 files changed

Lines changed: 103 additions & 466 deletions

File tree

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ println('Java: ' + System.getProperty('java.version') + ' JVM: ' + System.getPro
2121

2222
minecraft {
2323
mappings channel: 'official', version: '1.16.5'
24+
accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg')
2425
runs {
2526
client {
2627
workingDirectory project.file('run')

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
# This is required to provide enough memory for the Minecraft decompilation process.
33
org.gradle.jvmargs=-Xmx3G
44
org.gradle.daemon=false
5-
mod_version=1.16.5-3.0.2
5+
mod_version=1.16.5-3.1.0.0
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package net.polarfox27.jobs.containers;
2+
3+
import net.minecraft.entity.player.ServerPlayerEntity;
4+
import net.minecraft.inventory.CraftResultInventory;
5+
import net.minecraft.item.crafting.IRecipe;
6+
import net.minecraft.world.World;
7+
import net.polarfox27.jobs.data.ServerJobsData;
8+
import net.polarfox27.jobs.data.capabilities.PlayerData;
9+
import net.polarfox27.jobs.data.capabilities.PlayerJobs;
10+
11+
public class BlockingCraftingResultContainer extends CraftResultInventory {
12+
13+
14+
@Override
15+
public boolean setRecipeUsed(World world, ServerPlayerEntity player, IRecipe<?> recipe) {
16+
if(!super.setRecipeUsed(world, player, recipe))
17+
return false;
18+
19+
PlayerJobs jobs = PlayerData.getPlayerJobs(player);
20+
if(ServerJobsData.BLOCKED_CRAFTS.isBlocked(jobs, recipe.getResultItem())){
21+
this.setRecipeUsed(null);
22+
return false;
23+
}
24+
return true;
25+
}
26+
}

src/main/java/net/polarfox27/jobs/events/CommonEvents.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import net.polarfox27.jobs.data.ServerJobsData;
1717
import net.polarfox27.jobs.data.capabilities.PlayerData;
1818
import net.polarfox27.jobs.data.capabilities.PlayerJobs;
19+
import net.polarfox27.jobs.events.server.ContainerEvents;
1920
import net.polarfox27.jobs.network.PacketAskClientUpdate;
2021
import net.polarfox27.jobs.util.handler.PacketHandler;
2122

@@ -69,7 +70,10 @@ public void onEntityCloned(PlayerEvent.Clone event) {
6970
public void onPlayerJoinedServer(EntityJoinWorldEvent event){
7071
if(!(event.getEntity() instanceof ServerPlayerEntity))
7172
return;
73+
ServerPlayerEntity player = (ServerPlayerEntity)event.getEntity();
7274
ServerJobsData.sendDataToClient((ServerPlayerEntity)event.getEntity());
75+
if(player.gameMode.isSurvival())
76+
ContainerEvents.changePlayerInventoryCraftingSlot(player);
7377
}
7478

7579
/**

src/main/java/net/polarfox27/jobs/events/client/GuiEvents.java

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,14 @@
22

33
import com.mojang.blaze3d.matrix.MatrixStack;
44
import com.mojang.datafixers.util.Pair;
5-
import net.minecraft.block.Blocks;
65
import net.minecraft.client.Minecraft;
7-
import net.minecraft.entity.player.PlayerEntity;
8-
import net.minecraft.entity.player.PlayerInventory;
9-
import net.minecraft.inventory.container.Container;
10-
import net.minecraft.inventory.container.INamedContainerProvider;
11-
import net.minecraft.util.IWorldPosCallable;
12-
import net.minecraft.util.math.BlockPos;
13-
import net.minecraft.util.text.ITextComponent;
14-
import net.minecraft.util.text.TranslationTextComponent;
15-
import net.minecraft.world.World;
166
import net.minecraftforge.api.distmarker.Dist;
177
import net.minecraftforge.api.distmarker.OnlyIn;
188
import net.minecraftforge.client.event.RenderGameOverlayEvent;
19-
import net.minecraftforge.event.entity.player.PlayerInteractEvent.RightClickBlock;
209
import net.minecraftforge.eventbus.api.SubscribeEvent;
2110
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
2211
import net.polarfox27.jobs.data.ClientJobsData;
2312
import net.polarfox27.jobs.gui.GuiGainXP;
24-
import net.polarfox27.jobs.gui.containers.ContainerCraft;
2513

2614
@EventBusSubscriber
2715
public class GuiEvents {
@@ -46,40 +34,4 @@ public void clientTick(RenderGameOverlayEvent e) {
4634
gui.render(new MatrixStack());
4735
}
4836
}
49-
50-
/**
51-
* Cancels the opening of the regular crafting interface and opens the custom one from the Jobs mod.
52-
* @param e the Click Event
53-
*/
54-
@SubscribeEvent
55-
public void onOpenCraftingTable(RightClickBlock e) {
56-
if(e.getWorld().getBlockState(e.getPos()).getBlock() == Blocks.CRAFTING_TABLE) {
57-
e.setCanceled(true);
58-
if(!e.getWorld().isClientSide) {
59-
openUpdatedCraftingTable(e.getPlayer(), e.getWorld(), e.getPos());
60-
}
61-
}
62-
}
63-
64-
65-
/**
66-
* Opens the custom crafting interface of the Jobs mod.
67-
* @param player the player opening the interface
68-
* @param world the current world
69-
* @param pos the position of the crafting table
70-
*/
71-
private void openUpdatedCraftingTable(PlayerEntity player, World world, BlockPos pos){
72-
player.openMenu(new INamedContainerProvider() {
73-
74-
@Override
75-
public Container createMenu(int index, PlayerInventory inventory, PlayerEntity player) {
76-
return new ContainerCraft(index, inventory, IWorldPosCallable.create(world, pos));
77-
}
78-
79-
@Override
80-
public ITextComponent getDisplayName() {
81-
return new TranslationTextComponent("container.crafting");
82-
}
83-
});
84-
}
8537
}

src/main/java/net/polarfox27/jobs/events/server/BlockInteractionEvents.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import net.minecraft.block.BlockState;
55
import net.minecraft.block.CropsBlock;
66
import net.minecraft.entity.player.ServerPlayerEntity;
7+
import net.minecraft.item.ItemStack;
8+
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
79
import net.minecraftforge.event.world.BlockEvent.BreakEvent;
810
import net.minecraftforge.eventbus.api.SubscribeEvent;
911
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
@@ -49,6 +51,23 @@ public void onBreakOreOrCrop(BreakEvent event) {
4951
if(xp2 > 0)
5052
jobs.gainXP(job, xp2, player);
5153
}
54+
}
5255

56+
/**
57+
* Fired when a player right-clicks a block. If the player can't right-click using the item, event is canceled.
58+
* @param event the right click event.
59+
*/
60+
@SubscribeEvent
61+
public void onRightClickBlock(PlayerInteractEvent.RightClickBlock event){
62+
if(event.getWorld().isClientSide() || !(event.getPlayer() instanceof ServerPlayerEntity))
63+
return;
64+
ServerPlayerEntity player = (ServerPlayerEntity)event.getPlayer();
65+
PlayerJobs jobs = PlayerData.getPlayerJobs(player);
66+
67+
ItemStack stack = player.getMainHandItem() == ItemStack.EMPTY ? player.getOffhandItem() :
68+
player.getMainHandItem();
69+
70+
if(stack != ItemStack.EMPTY && ServerJobsData.BLOCKED_RIGHT_CLICKS.isBlocked(PlayerData.getPlayerJobs(player), stack))
71+
event.setCanceled(true);
5372
}
5473
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package net.polarfox27.jobs.events.server;
2+
3+
import net.minecraft.entity.player.PlayerEntity;
4+
import net.minecraft.inventory.container.CraftingResultSlot;
5+
import net.minecraft.inventory.container.PlayerContainer;
6+
import net.minecraft.inventory.container.WorkbenchContainer;
7+
import net.minecraftforge.event.entity.player.PlayerContainerEvent;
8+
import net.minecraftforge.event.entity.player.PlayerEvent;
9+
import net.minecraftforge.eventbus.api.SubscribeEvent;
10+
import net.polarfox27.jobs.containers.BlockingCraftingResultContainer;
11+
12+
public class ContainerEvents {
13+
14+
/**
15+
* Modify the crafting container when opened to block certain crafts.
16+
* @param e the Container opened event
17+
*/
18+
@SubscribeEvent
19+
public void onCraftingMenuOpened(PlayerContainerEvent.Open e) {
20+
if(e.getContainer() instanceof WorkbenchContainer){
21+
WorkbenchContainer container = (WorkbenchContainer)e.getContainer();
22+
container.resultSlots = new BlockingCraftingResultContainer();
23+
container.slots.set(0, new CraftingResultSlot(e.getPlayer(), container.craftSlots, container.resultSlots, 0, 124, 35));
24+
}
25+
}
26+
27+
/**
28+
* When the player is in Survival, modify the inventory to block certain crafts.
29+
* @param e the Click Event
30+
*/
31+
@SubscribeEvent
32+
public void onPlayerInventoryOpened(PlayerEvent.PlayerChangeGameModeEvent e) {
33+
if(e.getNewGameMode().isSurvival()){
34+
changePlayerInventoryCraftingSlot(e.getPlayer());
35+
}
36+
}
37+
38+
/**
39+
* Modifies the player inventory to block crafts according to the Jobs.
40+
* @param player the player from whom the inventory is modified.
41+
*/
42+
public static void changePlayerInventoryCraftingSlot(PlayerEntity player){
43+
PlayerContainer container = player.inventoryMenu;
44+
container.resultSlots = new BlockingCraftingResultContainer();
45+
container.slots.set(0, new CraftingResultSlot(player, container.getCraftSlots(), container.resultSlots, 0, 124, 35));
46+
}
47+
}

0 commit comments

Comments
 (0)