Skip to content

Commit

Permalink
What if name changing was overengineered? I'm always saying this
Browse files Browse the repository at this point in the history
  • Loading branch information
quat1024 committed Nov 10, 2023
1 parent ddce915 commit 0d47d6d
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 73 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.violetmoon.quark.content.building.module;

import org.violetmoon.quark.base.config.Config;
import org.violetmoon.quark.base.handler.ItemOverrideHandler;
import org.violetmoon.quark.base.util.VanillaWoods;
import org.violetmoon.quark.base.util.VanillaWoods.Wood;
import org.violetmoon.quark.content.building.block.VariantBookshelfBlock;
Expand All @@ -26,6 +25,6 @@ public final void register(ZRegister event) {

@LoadEvent
public final void configChanged(ZConfigChanged event) {
ItemOverrideHandler.changeBlockLocalizationKey(Blocks.BOOKSHELF, "block.quark.oak_bookshelf", changeNames && enabled);
zeta.nameChanger.changeBlock(Blocks.BOOKSHELF, "block.quark.oak_bookshelf", changeNames && enabled);
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
package org.violetmoon.quark.content.building.module;

import net.minecraft.core.BlockPos;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.LadderBlock;
import net.minecraft.world.level.block.TrapDoorBlock;
import net.minecraft.world.level.block.state.BlockState;

import java.util.LinkedList;
import java.util.List;

import org.violetmoon.quark.base.config.Config;
import org.violetmoon.quark.base.handler.FuelHandler;
import org.violetmoon.quark.base.handler.ItemOverrideHandler;
import org.violetmoon.quark.base.util.VanillaWoods;
import org.violetmoon.quark.base.util.VanillaWoods.Wood;
import org.violetmoon.quark.content.building.block.VariantLadderBlock;
Expand Down Expand Up @@ -47,21 +41,7 @@ public void loadComplete(ZLoadComplete e) {
@LoadEvent
public final void configChanged(ZConfigChanged event) {
moduleEnabled = this.enabled;
ItemOverrideHandler.changeBlockLocalizationKey(Blocks.LADDER, "block.quark.oak_ladder", changeNames && enabled);
}

public static boolean isTrapdoorLadder(boolean defaultValue, LevelReader world, BlockPos pos) {
if(defaultValue || !moduleEnabled)
return defaultValue;

BlockState curr = world.getBlockState(pos);
if(curr.getProperties().contains(TrapDoorBlock.OPEN) && curr.getValue(TrapDoorBlock.OPEN)) {
BlockState down = world.getBlockState(pos.below());
if(down.getBlock() instanceof LadderBlock)
return down.getValue(LadderBlock.FACING) == curr.getValue(TrapDoorBlock.FACING);
}

return false;
zeta.nameChanger.changeBlock(Blocks.LADDER, "block.quark.oak_ladder", changeNames && enabled);
}

}
6 changes: 6 additions & 0 deletions src/main/java/org/violetmoon/zeta/Zeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;
import org.violetmoon.quark.base.handler.GeneralConfig;
import org.violetmoon.zeta.util.NameChanger;
import org.violetmoon.zeta.util.RaytracingUtil;
import org.violetmoon.zeta.advancement.AdvancementModifierRegistry;
import org.violetmoon.zeta.block.ext.BlockExtensionFactory;
Expand Down Expand Up @@ -59,6 +60,7 @@ public Zeta(String modid, Logger log, ZetaSide side) {
this.capabilityManager = createCapabilityManager();

this.raytracingUtil = createRaytracingUtil();
this.nameChanger = createNameChanger();

loadBus.subscribe(craftingExtensions)
.subscribe(dyeables)
Expand Down Expand Up @@ -86,6 +88,7 @@ public Zeta(String modid, Logger log, ZetaSide side) {
public final ItemExtensionFactory itemExtensions;

public final RaytracingUtil raytracingUtil;
public final NameChanger nameChanger;

public ConfigManager configManager; //This could do with being split up into various pieces?
public IZetaConfigInternals configInternals;
Expand Down Expand Up @@ -134,6 +137,9 @@ public BlockExtensionFactory createBlockExtensionFactory() {
}
public abstract ItemExtensionFactory createItemExtensionFactory();
public abstract RaytracingUtil createRaytracingUtil();
public NameChanger createNameChanger() {
return new NameChanger();
}

public abstract <E, T extends E> T fireExternalEvent(T impl);

Expand Down
100 changes: 100 additions & 0 deletions src/main/java/org/violetmoon/zeta/util/NameChanger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package org.violetmoon.zeta.util;

import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;

import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;

public class NameChanger {

/**
* Submit or rescind (depending on `enabled`) a request to change the translation key of a block.
* <p>
* The most recently submitted name change request will win. If there are no outstanding requests,
* the block will return to its original vanilla name.
*/
public void changeBlock(Block toChange, String newTranslationKey, boolean enabled) {
changeBlockStatic(toChange, newTranslationKey, enabled);
}

public void changeItem(Item toChange, String newTranslationKey, boolean enabled) {
changeItemStatic(toChange, newTranslationKey, enabled);
}

// this stuff is implemented statically so that originalBlockNames really does contain the *original* block names.
// if this wasn't static, you could imagine one Zeta mod changes the name of a block, then another mod changes
// the name of the same block; it'd end up thinking the first mod's *changed* name is the *original* name.

// Most of the complexity of this class is about handling cases like 'mod A changes block name, mod B changes
// the same block name, mod A removes its name-change" -> we should select mod B's name.

protected static Map<Block, String> originalBlockNames = new IdentityHashMap<>();
protected static Map<Block, NameChangeRequests> changedBlockNames = new IdentityHashMap<>();
protected static Map<Item, String> originalItemNames = new IdentityHashMap<>();
protected static Map<Item, NameChangeRequests> changedItemNames = new IdentityHashMap<>();

// marked "synchronized" cause uhhhh ?? forge??... idk... might be a good idea
protected static synchronized void changeBlockStatic(Block toChange, String newTranslationKey, boolean enabled) {
//keep track of the original name for this block
originalBlockNames.computeIfAbsent(toChange, Block::getDescriptionId);

//add the changed name onto the pile
NameChangeRequests changeRequests = changedBlockNames.computeIfAbsent(toChange, __ -> new NameChangeRequests());
if(enabled)
changeRequests.add(newTranslationKey);
else
changeRequests.remove(newTranslationKey);

//actually change the block's name - if there are any outstanding name-change requests, use the most recent one,
//else use the block's original name
toChange.descriptionId = changeRequests.lastOrElse(originalBlockNames.get(toChange));

//save a tiny bit of memory
if(changeRequests.isEmpty())
changedBlockNames.remove(toChange);
}

protected static synchronized void changeItemStatic(Item toChange, String newTranslationKey, boolean enabled) {
originalItemNames.computeIfAbsent(toChange, Item::getDescriptionId);

NameChangeRequests changeRequests = changedItemNames.computeIfAbsent(toChange, __ -> new NameChangeRequests());
if(enabled)
changeRequests.add(newTranslationKey);
else
changeRequests.remove(newTranslationKey);
toChange.descriptionId = changeRequests.lastOrElse(originalItemNames.get(toChange));

if(changeRequests.isEmpty())
changedItemNames.remove(toChange);
}

// In practice these collections will contain like, 1 element at-most, *maybe* 2, so the O(n) algorithm choice is intentional
// Bro i,m starting to think i overengineered this class :skull:
protected static class NameChangeRequests {
List<String> list = new ArrayList<>(1);

public void add(String value) {
// move it to the end, so the most recent requests get prioritized
remove(value);
list.add(value);
}

public void remove(String value) {
list.remove(value);
}

public boolean isEmpty() {
return list.isEmpty();
}

public String lastOrElse(String orElse) {
if(list.isEmpty())
return orElse;
else
return list.get(list.size() - 1);
}
}
}
2 changes: 1 addition & 1 deletion zeta-todo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Bring to zeta:
- [ ] "ig the worldgen shim?"
- [ ] "pretty much everything in `block` is viable to pull out"
- [x] some stuff wrt to module loader - anti overlap
- [ ] ItemOverrideHandler is used by variant bookshelves/ladders but it's pretty standalone
- [x] ItemOverrideHandler is used by variant bookshelves/ladders but it's pretty standalone
- [ ] ToolInteractionHandler is "literally only used for waxing" but it's important
- [ ] QuarkBlock and QuarkItem are really for "disableable/enablable blocks"
- [ ] WoodSetHandler is important, but there are some quark uniques in there
Expand Down

0 comments on commit 0d47d6d

Please sign in to comment.