Skip to content

Commit

Permalink
compass, data part should be done
Browse files Browse the repository at this point in the history
  • Loading branch information
warior456 committed Jan 28, 2025
1 parent a3e0f35 commit 615fd08
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,86 +73,76 @@ public void inventoryTick(ItemStack stack, World world, Entity entity, int slot,
if(oscillatorTrackerComponentList == null) return;

List<OscillatorTrackerComponent> trackers = oscillatorTrackerComponentList.trackers();
if(trackers.isEmpty()) return;
List<OscillatorTrackerComponent> trackers1 = new ArrayList<OscillatorTrackerComponent>(trackers);//DO NOT DELETE AT ANY COST

int selectedLocation = oscillatorTrackerComponentList.selectedLocation();

if(selectedLocation >= trackers.size()) {//outofbounds check
if(selectedLocation >= trackers.size() || selectedLocation<0) {//outofbounds check
selectedLocation = 0;
}
/* System.out.println("trackers size: "+trackers.size());
System.out.println("selected: "+ selectedLocation);*/
if (trackers.get(selectedLocation) != null) {
OscillatorTrackerComponent oscillatorTrackerComponent1 = trackers.get(selectedLocation);
OscillatorTrackerComponent oscillatorTrackerComponent2 = oscillatorTrackerComponent1.forWorld(serverWorld);
if(oscillatorTrackerComponent != oscillatorTrackerComponent1) {
stack.set(ModComponentTypes.OSCILLATOR_TRACKER, oscillatorTrackerComponent1);
stack.set(ModComponentTypes.OSCILLATOR_TRACKER_LIST, new OscillatorTrackerComponentList(selectedLocation, trackers));
}
if(oscillatorTrackerComponent1 != oscillatorTrackerComponent2) {
//System.out.println();
stack.set(ModComponentTypes.OSCILLATOR_TRACKER, oscillatorTrackerComponent2);
trackers.remove(selectedLocation);
stack.set(ModComponentTypes.OSCILLATOR_TRACKER_LIST, new OscillatorTrackerComponentList(selectedLocation-1, trackers));

if(trackers.get(selectedLocation) == null) return;

OscillatorTrackerComponent oscillatorTrackerComponent1 = trackers.get(selectedLocation);
OscillatorTrackerComponent oscillatorTrackerComponent2 = oscillatorTrackerComponent1.forWorld(serverWorld);
if(oscillatorTrackerComponent != oscillatorTrackerComponent1) {
stack.set(ModComponentTypes.OSCILLATOR_TRACKER, oscillatorTrackerComponent1);
stack.set(ModComponentTypes.OSCILLATOR_TRACKER_LIST, new OscillatorTrackerComponentList(selectedLocation, trackers));
}
if(oscillatorTrackerComponent1 != oscillatorTrackerComponent2) {
//System.out.println();
stack.set(ModComponentTypes.OSCILLATOR_TRACKER, oscillatorTrackerComponent2);
System.out.println(trackers);
System.out.println(selectedLocation);
trackers1.remove(selectedLocation);
if(selectedLocation == 0) {
stack.set(ModComponentTypes.OSCILLATOR_TRACKER_LIST, new OscillatorTrackerComponentList(selectedLocation, trackers1));
return;
}
stack.set(ModComponentTypes.OSCILLATOR_TRACKER_LIST, new OscillatorTrackerComponentList(selectedLocation-1, trackers1));
}

}
}


@Override
public ActionResult useOnBlock(ItemUsageContext context) {
List<OscillatorTrackerComponent> trackers = new ArrayList<OscillatorTrackerComponent>();
System.out.println("START USE ON BLOCK");

BlockPos blockPos = context.getBlockPos();
World world = context.getWorld();
if (!world.getBlockState(blockPos).isOf(QUAZARITH_OSCILLATOR)) {
return super.useOnBlock(context);
} else {
if(context.getWorld().isClient) return ActionResult.PASS;//maybe error spam fix?
world.playSound(null, blockPos, SoundEvents.ITEM_LODESTONE_COMPASS_LOCK, SoundCategory.PLAYERS, 1.0F, 1.0F);
PlayerEntity playerEntity = context.getPlayer();
ItemStack itemStack = context.getStack();
boolean bl = !playerEntity.isInCreativeMode() && itemStack.getCount() == 1;

List<OscillatorTrackerComponent> trackers = new ArrayList<OscillatorTrackerComponent>();
int selectedLocation = 0;
if(itemStack.get(ModComponentTypes.OSCILLATOR_TRACKER_LIST) != null) {
trackers = itemStack.get(ModComponentTypes.OSCILLATOR_TRACKER_LIST).trackers();
selectedLocation = itemStack.get(ModComponentTypes.OSCILLATOR_TRACKER_LIST).selectedLocation();
};


OscillatorTrackerComponent oscillatorTrackerComponent = new OscillatorTrackerComponent(Optional.of(GlobalPos.create(world.getRegistryKey(), blockPos)), true);
if (bl) {

trackers.add(oscillatorTrackerComponent);
System.out.println(trackers);
OscillatorTrackerComponentList oscillatorTrackerComponentList = new OscillatorTrackerComponentList(selectedLocation, trackers);
System.out.println(oscillatorTrackerComponentList);
itemStack.set(ModComponentTypes.OSCILLATOR_TRACKER , oscillatorTrackerComponentList.trackers().get(oscillatorTrackerComponentList.trackers().size() - 1));
itemStack.set(ModComponentTypes.OSCILLATOR_TRACKER_LIST , oscillatorTrackerComponentList);

if (!world.getBlockState(blockPos).isOf(QUAZARITH_OSCILLATOR)) return super.useOnBlock(context);
//if (context.getWorld().isClient) return ActionResult.PASS;//maybe error spam fix?
world.playSound(null, blockPos, SoundEvents.ITEM_LODESTONE_COMPASS_LOCK, SoundCategory.PLAYERS, 1.0F, 1.0F);

//itemStack.apply(ModComponentTypes.OSCILLATOR_TRACKER_LIST, new OscillatorTrackerComponentList(0, trackers), new OscillatorTrackerComponentList(0,));
PlayerEntity playerEntity = context.getPlayer();
ItemStack itemStack = context.getStack();

if(itemStack.get(ModComponentTypes.OSCILLATOR_TRACKER_LIST) != null) {
trackers = itemStack.get(ModComponentTypes.OSCILLATOR_TRACKER_LIST).trackers();
System.out.println("trackers:" + trackers);
};

OscillatorTrackerComponent oscillatorTrackerComponent = new OscillatorTrackerComponent(Optional.of(GlobalPos.create(world.getRegistryKey(), blockPos)), true, "");

} else {
ItemStack itemStack2 = itemStack.copyComponentsToNewStack(ModItems.CRUX_RESONATOR, 1);
itemStack.decrementUnlessCreative(1, playerEntity);

trackers.add(oscillatorTrackerComponent);
System.out.println("2");
System.out.println(trackers);
OscillatorTrackerComponentList oscillatorTrackerComponentList = new OscillatorTrackerComponentList(selectedLocation, trackers);
System.out.println(oscillatorTrackerComponentList);
itemStack.set(ModComponentTypes.OSCILLATOR_TRACKER , oscillatorTrackerComponentList.trackers().get(oscillatorTrackerComponentList.trackers().size() - 1));
itemStack2.set(ModComponentTypes.OSCILLATOR_TRACKER_LIST, oscillatorTrackerComponentList);
List<OscillatorTrackerComponent> trackers1 = new ArrayList<OscillatorTrackerComponent>(trackers);//DO NOT TOUCH AT ANY COST
trackers1.addFirst(oscillatorTrackerComponent);

OscillatorTrackerComponentList oscillatorTrackerComponentList = new OscillatorTrackerComponentList(0, trackers1);

if (!playerEntity.getInventory().insertStack(itemStack2)) {
playerEntity.dropItem(itemStack2, false);
}
}
itemStack.set(ModComponentTypes.OSCILLATOR_TRACKER , oscillatorTrackerComponent);
itemStack.set(ModComponentTypes.OSCILLATOR_TRACKER_LIST , oscillatorTrackerComponentList);


System.out.println("END USE ON BLOCK");

return ActionResult.success(world.isClient);

return ActionResult.success(world.isClient);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
import java.util.Optional;
import java.util.function.Consumer;

public record OscillatorTrackerComponent(Optional<GlobalPos> target, boolean tracked) implements TooltipAppender {
public record OscillatorTrackerComponent(Optional<GlobalPos> target, boolean tracked, String name) implements TooltipAppender {
public static final Codec<OscillatorTrackerComponent> CODEC = RecordCodecBuilder.create(
instance -> instance.group(
GlobalPos.CODEC.optionalFieldOf("target").forGetter(OscillatorTrackerComponent::target),
Codec.BOOL.optionalFieldOf("tracked", Boolean.valueOf(true)).forGetter(OscillatorTrackerComponent::tracked)
Codec.BOOL.optionalFieldOf("tracked", Boolean.valueOf(true)).forGetter(OscillatorTrackerComponent::tracked),
Codec.STRING.optionalFieldOf("name","").forGetter(OscillatorTrackerComponent::name)
)
.apply(instance, OscillatorTrackerComponent::new)
);
Expand All @@ -34,6 +35,8 @@ public record OscillatorTrackerComponent(Optional<GlobalPos> target, boolean tra
OscillatorTrackerComponent::target,
PacketCodecs.BOOL,
OscillatorTrackerComponent::tracked,
PacketCodecs.STRING,
OscillatorTrackerComponent::name,
OscillatorTrackerComponent::new
);

Expand All @@ -45,7 +48,7 @@ public OscillatorTrackerComponent forWorld(ServerWorld world) {
BlockPos blockPos = ((GlobalPos)this.target.get()).pos();
return world.isInBuildLimit(blockPos) && world.getPointOfInterestStorage().hasTypeAt(POIs.QUAZARITH_OSCILLATOR_POI, blockPos)
? this
: new OscillatorTrackerComponent(Optional.empty(), true);
: new OscillatorTrackerComponent(Optional.empty(), true, "");
}
} else {
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public record OscillatorTrackerComponentList(int selectedLocation, List<Oscillat
public static final int MAX_EXPLOSIONS = 256;
public static final Codec<OscillatorTrackerComponentList> CODEC = RecordCodecBuilder.create(
instance -> instance.group(
Codecs.UNSIGNED_BYTE.optionalFieldOf("selected_location", 0).forGetter(OscillatorTrackerComponentList::selectedLocation),
Codecs.POSITIVE_INT.optionalFieldOf("selected_location", 0).forGetter(OscillatorTrackerComponentList::selectedLocation),
OscillatorTrackerComponent.CODEC.sizeLimitedListOf(256).optionalFieldOf("trackers", List.of()).forGetter(OscillatorTrackerComponentList::trackers)
)
.apply(instance, OscillatorTrackerComponentList::new)
);
public static final PacketCodec<ByteBuf, OscillatorTrackerComponentList> PACKET_CODEC = PacketCodec.tuple(
PacketCodecs.VAR_INT,
OscillatorTrackerComponentList::selectedLocation,
OscillatorTrackerComponent.PACKET_CODEC.collect(PacketCodecs.toList(256)), //todo eeee
OscillatorTrackerComponent.PACKET_CODEC.collect(PacketCodecs.toList(256)), //todo set lower limit
OscillatorTrackerComponentList::trackers,
OscillatorTrackerComponentList::new
);
Expand Down

0 comments on commit 615fd08

Please sign in to comment.