Skip to content

Commit

Permalink
Replace several Guava classes with Java stdlib
Browse files Browse the repository at this point in the history
Wow, some of this is /old/. All the Maps.newHashMap stuff dates back to
Java 6, so must originally be CCTweaks code?!

We're unlikely to drop our Guava dependency (we use too much other
stuff), but we should make the most of the stdlib where possible.
  • Loading branch information
SquidDev committed Oct 11, 2023
1 parent 7e5598d commit bdce9a8
Show file tree
Hide file tree
Showing 17 changed files with 89 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.annotation.Nullable;
import java.util.*;
Expand All @@ -36,8 +34,6 @@
* @param <R> The upgrade serialiser to register for.
*/
public abstract class UpgradeDataProvider<T extends UpgradeBase, R extends UpgradeSerialiser<? extends T>> implements DataProvider {
private static final Logger LOGGER = LogManager.getLogger();

private final PackOutput output;
private final String name;
private final String folder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
import dan200.computercraft.core.util.Colour;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.server.packs.resources.ResourceProvider;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL31;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;
import java.io.IOException;
Expand All @@ -36,12 +36,12 @@
* @see RenderTypes#getMonitorTextureBufferShader()
*/
public class MonitorTextureBufferShader extends ShaderInstance {
private static final Logger LOG = LoggerFactory.getLogger(MonitorTextureBufferShader.class);

public static final int UNIFORM_SIZE = 4 * 4 * 16 + 4 + 4 + 2 * 4 + 4;

static final int TEXTURE_INDEX = GL13.GL_TEXTURE3;

private static final Logger LOGGER = LogManager.getLogger();

private final int monitorData;
private int uniformBuffer = 0;

Expand Down Expand Up @@ -75,7 +75,7 @@ public void apply() {
private Uniform getUniformChecked(String name) {
var uniform = getUniform(name);
if (uniform == null) {
LOGGER.warn("Monitor shader {} should have uniform {}, but it was not present.", getName(), name);
LOG.warn("Monitor shader {} should have uniform {}, but it was not present.", getName(), name);
}

return uniform;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import net.minecraft.util.GsonHelper;
import net.minecraft.util.profiling.ProfilerFiller;
import net.minecraft.world.item.ItemStack;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;
import java.util.Collection;
Expand All @@ -37,7 +37,7 @@
* @see PocketUpgrades
*/
public class UpgradeManager<R extends UpgradeSerialiser<? extends T>, T extends UpgradeBase> extends SimpleJsonResourceReloadListener {
private static final Logger LOGGER = LogManager.getLogger();
private static final Logger LOG = LoggerFactory.getLogger(UpgradeManager.class);
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();

public record UpgradeWrapper<R extends UpgradeSerialiser<? extends T>, T extends UpgradeBase>(
Expand Down Expand Up @@ -103,13 +103,13 @@ protected void apply(Map<ResourceLocation, JsonElement> upgrades, ResourceManage
try {
loadUpgrade(newUpgrades, element.getKey(), element.getValue());
} catch (IllegalArgumentException | JsonParseException e) {
LOGGER.error("Error loading {} {} from JSON file", kind, element.getKey(), e);
LOG.error("Error loading {} {} from JSON file", kind, element.getKey(), e);
}
}

current = Collections.unmodifiableMap(newUpgrades);
currentWrappers = newUpgrades.values().stream().collect(Collectors.toUnmodifiableMap(UpgradeWrapper::upgrade, x -> x));
LOGGER.info("Loaded {} {}s", current.size(), kind);
LOG.info("Loaded {} {}s", current.size(), kind);
}

private void loadUpgrade(Map<String, UpgradeWrapper<R, T>> current, ResourceLocation id, JsonElement json) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package dan200.computercraft.impl.network.wired;

import com.google.common.collect.ImmutableMap;
import dan200.computercraft.api.network.Packet;
import dan200.computercraft.api.network.wired.WiredNetwork;
import dan200.computercraft.api.network.wired.WiredNode;
Expand Down Expand Up @@ -260,7 +259,7 @@ public void updatePeripherals(WiredNode node, Map<String, IPeripheral> newPeriph
var change = WiredNetworkChangeImpl.changeOf(oldPeripherals, newPeripherals);
if (change.isEmpty()) return;

wired.peripherals = ImmutableMap.copyOf(newPeripherals);
wired.peripherals = Map.copyOf(newPeripherals);

// Detach the old peripherals then remove them.
peripherals.keySet().removeAll(change.peripheralsRemoved().keySet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
import dan200.computercraft.api.peripheral.IPeripheral;

import javax.annotation.Nullable;
import java.util.*;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

public final class WiredNodeImpl implements WiredNode {
private @Nullable Set<PacketReceiver> receivers;

final WiredElement element;
Map<String, IPeripheral> peripherals = Collections.emptyMap();
Map<String, IPeripheral> peripherals = Map.of();

final HashSet<WiredNodeImpl> neighbours = new HashSet<>();
volatile WiredNetworkImpl network;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

package dan200.computercraft.shared.peripheral.modem.wired;

import com.google.common.collect.ImmutableMap;
import dan200.computercraft.annotations.ForgeOverride;
import dan200.computercraft.shared.ModRegistry;
import dan200.computercraft.shared.platform.PlatformHelper;
import dan200.computercraft.shared.util.WaterloggableHelpers;
import dan200.computercraft.shared.util.WorldUtil;
import net.minecraft.Util;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
Expand Down Expand Up @@ -52,12 +52,14 @@ public class CableBlock extends Block implements SimpleWaterloggedBlock, EntityB
public static final BooleanProperty UP = BooleanProperty.create("up");
public static final BooleanProperty DOWN = BooleanProperty.create("down");

static final EnumMap<Direction, BooleanProperty> CONNECTIONS =
new EnumMap<>(new ImmutableMap.Builder<Direction, BooleanProperty>()
.put(Direction.DOWN, DOWN).put(Direction.UP, UP)
.put(Direction.NORTH, NORTH).put(Direction.SOUTH, SOUTH)
.put(Direction.WEST, WEST).put(Direction.EAST, EAST)
.build());
static final EnumMap<Direction, BooleanProperty> CONNECTIONS = Util.make(new EnumMap<>(Direction.class), m -> {
m.put(Direction.DOWN, DOWN);
m.put(Direction.UP, UP);
m.put(Direction.NORTH, NORTH);
m.put(Direction.SOUTH, SOUTH);
m.put(Direction.WEST, WEST);
m.put(Direction.EAST, EAST);
});

public CableBlock(Properties settings) {
super(settings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package dan200.computercraft.shared.peripheral.modem.wired;

import com.google.common.base.Objects;
import dan200.computercraft.api.network.wired.WiredElement;
import dan200.computercraft.api.network.wired.WiredNode;
import dan200.computercraft.api.peripheral.IPeripheral;
Expand Down Expand Up @@ -32,6 +31,7 @@

import javax.annotation.Nullable;
import java.util.Collections;
import java.util.Objects;

public class CableBlockEntity extends BlockEntity {
private static final String NBT_PERIPHERAL_ENABLED = "PeripheralAccess";
Expand Down Expand Up @@ -181,7 +181,7 @@ InteractionResult use(Player player) {
var oldName = peripheral.getConnectedName();
togglePeripheralAccess();
var newName = peripheral.getConnectedName();
if (!Objects.equal(newName, oldName)) {
if (!Objects.equals(newName, oldName)) {
if (oldName != null) {
player.displayClientMessage(Component.translatable("chat.computercraft.wired_modem.peripheral_disconnected",
ChatHelpers.copy(oldName)), false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

package dan200.computercraft.shared.peripheral.modem.wired;

import com.google.common.collect.ImmutableMap;
import dan200.computercraft.shared.peripheral.modem.ModemShapes;
import dan200.computercraft.shared.util.DirectionUtil;
import net.minecraft.Util;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.shapes.Shapes;
Expand All @@ -21,16 +21,14 @@ public final class CableShapes {
private static final double MAX = 1 - MIN;

private static final VoxelShape SHAPE_CABLE_CORE = Shapes.box(MIN, MIN, MIN, MAX, MAX, MAX);
private static final EnumMap<Direction, VoxelShape> SHAPE_CABLE_ARM =
new EnumMap<>(new ImmutableMap.Builder<Direction, VoxelShape>()
.put(Direction.DOWN, Shapes.box(MIN, 0, MIN, MAX, MIN, MAX))
.put(Direction.UP, Shapes.box(MIN, MAX, MIN, MAX, 1, MAX))
.put(Direction.NORTH, Shapes.box(MIN, MIN, 0, MAX, MAX, MIN))
.put(Direction.SOUTH, Shapes.box(MIN, MIN, MAX, MAX, MAX, 1))
.put(Direction.WEST, Shapes.box(0, MIN, MIN, MIN, MAX, MAX))
.put(Direction.EAST, Shapes.box(MAX, MIN, MIN, 1, MAX, MAX))
.build()
);
private static final EnumMap<Direction, VoxelShape> SHAPE_CABLE_ARM = Util.make(new EnumMap<>(Direction.class), m -> {
m.put(Direction.DOWN, Shapes.box(MIN, 0, MIN, MAX, MIN, MAX));
m.put(Direction.UP, Shapes.box(MIN, MAX, MIN, MAX, 1, MAX));
m.put(Direction.NORTH, Shapes.box(MIN, MIN, 0, MAX, MAX, MIN));
m.put(Direction.SOUTH, Shapes.box(MIN, MIN, MAX, MAX, MAX, 1));
m.put(Direction.WEST, Shapes.box(0, MIN, MIN, MIN, MAX, MAX));
m.put(Direction.EAST, Shapes.box(MAX, MIN, MIN, 1, MAX, MAX));
});

private static final VoxelShape[] SHAPES = new VoxelShape[(1 << 6) * 7];
private static final VoxelShape[] CABLE_SHAPES = new VoxelShape[1 << 6];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package dan200.computercraft.shared.peripheral.modem.wired;

import com.google.common.base.Objects;
import dan200.computercraft.api.network.wired.WiredElement;
import dan200.computercraft.api.network.wired.WiredNode;
import dan200.computercraft.api.peripheral.IPeripheral;
Expand Down Expand Up @@ -133,7 +132,7 @@ public InteractionResult use(Player player) {
togglePeripheralAccess();
var periphNames = getConnectedPeripheralNames();

if (!Objects.equal(periphNames, oldPeriphNames)) {
if (!Objects.equals(periphNames, oldPeriphNames)) {
sendPeripheralChanges(player, "chat.computercraft.wired_modem.peripheral_disconnected", oldPeriphNames);
sendPeripheralChanges(player, "chat.computercraft.wired_modem.peripheral_connected", periphNames);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package dan200.computercraft.shared.peripheral.modem.wired;

import com.google.common.collect.ImmutableMap;
import dan200.computercraft.api.filesystem.Mount;
import dan200.computercraft.api.filesystem.WritableMount;
import dan200.computercraft.api.lua.*;
Expand Down Expand Up @@ -429,7 +428,7 @@ public String getAttachmentName() {
public Map<String, IPeripheral> getAvailablePeripherals() {
if (!attached) throw new NotAttachedException();
synchronized (element.getRemotePeripherals()) {
return ImmutableMap.copyOf(element.getRemotePeripherals());
return Map.copyOf(element.getRemotePeripherals());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package dan200.computercraft.shared.pocket.items;

import com.google.common.base.Objects;
import dan200.computercraft.annotations.ForgeOverride;
import dan200.computercraft.api.ComputerCraftAPI;
import dan200.computercraft.api.filesystem.Mount;
Expand Down Expand Up @@ -44,6 +43,7 @@

import javax.annotation.Nullable;
import java.util.List;
import java.util.Objects;

public class PocketComputerItem extends Item implements IComputerItem, IMedia, IColouredItem {
private static final String NBT_UPGRADE = "Upgrade";
Expand Down Expand Up @@ -97,7 +97,7 @@ private boolean tick(ItemStack stack, Level world, Entity entity, PocketServerCo

// Sync label
var label = computer.getLabel();
if (!Objects.equal(label, getLabel(stack))) {
if (!Objects.equals(label, getLabel(stack))) {
changed = true;
setLabel(stack, label);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package dan200.computercraft.shared.turtle.core;

import com.google.common.base.Objects;
import com.mojang.authlib.GameProfile;
import dan200.computercraft.api.lua.ILuaCallback;
import dan200.computercraft.api.lua.MethodResult;
Expand Down Expand Up @@ -455,7 +454,7 @@ public void playAnimation(TurtleAnimation animation) {
}

public void setOverlay(@Nullable ResourceLocation overlay) {
if (!Objects.equal(this.overlay, overlay)) {
if (!Objects.equals(this.overlay, overlay)) {
this.overlay = overlay;
BlockEntityHelpers.updateBlock(owner);
}
Expand Down Expand Up @@ -573,7 +572,7 @@ public Vec3 getRenderOffset(float f) {

public float getToolRenderAngle(TurtleSide side, float f) {
return (side == TurtleSide.LEFT && animation == TurtleAnimation.SWING_LEFT_TOOL) ||
(side == TurtleSide.RIGHT && animation == TurtleAnimation.SWING_RIGHT_TOOL)
(side == TurtleSide.RIGHT && animation == TurtleAnimation.SWING_RIGHT_TOOL)
? 45.0f * (float) Math.sin(getAnimationFraction(f) * Math.PI)
: 0.0f;
}
Expand Down
Loading

0 comments on commit bdce9a8

Please sign in to comment.