Skip to content

Commit

Permalink
State waxing thing
Browse files Browse the repository at this point in the history
  • Loading branch information
IThundxr committed Feb 16, 2024
1 parent 686617b commit ff305c3
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 16 deletions.
14 changes: 14 additions & 0 deletions src/main/java/org/violetmoon/zeta/block/ZetaWaxableStateBlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.violetmoon.zeta.block;

import net.minecraft.world.level.block.state.properties.BooleanProperty;
import org.jetbrains.annotations.Nullable;
import org.violetmoon.zeta.module.ZetaModule;

// haha yes im so good at naming stuff
public class ZetaWaxableStateBlock extends ZetaBlock {
public static final BooleanProperty ZETA_WAXED = BooleanProperty.create("zeta_waxed");

public ZetaWaxableStateBlock(String regname, @Nullable ZetaModule module, Properties properties) {
super(regname, module, properties);
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
package org.violetmoon.zeta.util.handler;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.tuple.Pair;
import org.violetmoon.zeta.advancement.modifier.WaxModifier;
import org.violetmoon.zeta.event.bus.LoadEvent;
import org.violetmoon.zeta.event.bus.PlayEvent;
import org.violetmoon.zeta.event.load.ZCommonSetup;
import org.violetmoon.zeta.event.play.ZBlock;
import org.violetmoon.zeta.event.play.entity.player.ZRightClickBlock;
import org.violetmoon.zeta.module.ZetaModule;

import com.google.common.collect.HashBiMap;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;

import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerPlayer;
Expand All @@ -32,11 +16,25 @@
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraftforge.common.ToolAction;
import net.minecraftforge.common.ToolActions;
import org.apache.commons.lang3.tuple.Pair;
import org.violetmoon.zeta.advancement.modifier.WaxModifier;
import org.violetmoon.zeta.block.ZetaWaxableStateBlock;
import org.violetmoon.zeta.event.bus.LoadEvent;
import org.violetmoon.zeta.event.bus.PlayEvent;
import org.violetmoon.zeta.event.load.ZCommonSetup;
import org.violetmoon.zeta.event.play.ZBlock;
import org.violetmoon.zeta.event.play.entity.player.ZRightClickBlock;
import org.violetmoon.zeta.module.ZetaModule;

import java.util.*;

public final class ToolInteractionHandler {

private static final Map<Block, Block> cleanToWaxMap = HashBiMap.create();
private static final Map<Block, BlockState> cleanToWaxMapState = HashBiMap.create();

private static final Map<ToolAction, Map<Block, Block>> interactionMaps = new HashMap<>();
private static final Map<ToolAction, Map<BlockState, Block>> interactionMapsState = new HashMap<>();

private static final Multimap<ZetaModule, Pair<Block, Block>> waxingByModule = HashMultimap.create();

Expand All @@ -47,6 +45,15 @@ public static void registerWaxedBlock(ZetaModule module, Block clean, Block waxe
waxingByModule.put(module, Pair.of(clean, waxed));
}

// BlockStates block must implement extend ZetaWaxableStateBlock for now.
public static void registerWaxedBlock(ZetaModule module, Block clean, BlockState waxedState) {
if (!(waxedState.getBlock() instanceof ZetaWaxableStateBlock))
throw new IllegalArgumentException("registerWaxedBlock(ZetaModule, Block, Blockstate) only supports ZetaWaxableStateBlock");

cleanToWaxMapState.put(clean, waxedState);
registerInteraction(ToolActions.AXE_WAX_OFF, waxedState, clean);
}

public static void registerInteraction(ToolAction action, Block in, Block out) {
if(!interactionMaps.containsKey(action))
interactionMaps.put(action, new HashMap<>());
Expand All @@ -55,6 +62,14 @@ public static void registerInteraction(ToolAction action, Block in, Block out) {
map.put(in, out);
}

public static void registerInteraction(ToolAction action, BlockState in, Block out) {
if(!interactionMapsState.containsKey(action))
interactionMapsState.put(action, new HashMap<>());

Map<BlockState, Block> map = interactionMapsState.get(action);
map.put(in, out);
}

@LoadEvent
public static void addModifiers(ZCommonSetup event) {
event.enqueueWork(() -> {
Expand Down Expand Up @@ -88,6 +103,16 @@ public static void toolActionEvent(ZBlock.BlockToolModification event) {
event.setFinalState(copyState(state, finalBlock));
}
}

if(interactionMapsState.containsKey(action)) {
Map<BlockState, Block> map = interactionMapsState.get(action);
BlockState state = event.getState();

if(map.containsKey(state)) {
Block finalBlock = map.get(state);
event.setFinalState(copyState(state, finalBlock).setValue(ZetaWaxableStateBlock.ZETA_WAXED, false));
}
}
}

@PlayEvent
Expand Down Expand Up @@ -116,6 +141,23 @@ public static void itemUse(ZRightClickBlock event) {
event.setCanceled(true);
event.setCancellationResult(InteractionResult.SUCCESS);
}

if(cleanToWaxMapState.containsKey(block)) {
BlockState alternate = cleanToWaxMapState.get(block);

if(event.getEntity() instanceof ServerPlayer sp)
CriteriaTriggers.ITEM_USED_ON_BLOCK.trigger(sp, pos, stack);

if(!world.isClientSide)
world.setBlockAndUpdate(pos, copyState(state, alternate.getBlock()).setValue(ZetaWaxableStateBlock.ZETA_WAXED, true));
world.levelEvent(event.getPlayer(), LevelEvent.PARTICLES_AND_SOUND_WAX_ON, pos, 0);

if(!event.getPlayer().getAbilities().instabuild)
stack.setCount(stack.getCount() - 1);

event.setCanceled(true);
event.setCancellationResult(InteractionResult.SUCCESS);
}
}
}

Expand Down

0 comments on commit ff305c3

Please sign in to comment.