Skip to content

Commit 09cf870

Browse files
committed
Add FastLogBlock item
1 parent 5b56ea1 commit 09cf870

File tree

12 files changed

+214
-10
lines changed

12 files changed

+214
-10
lines changed
Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,43 @@
11
package ru.lionzxy.fastlogblock;
22

3+
import net.minecraft.item.Item;
34
import net.minecraftforge.common.MinecraftForge;
5+
import net.minecraftforge.event.RegistryEvent;
46
import net.minecraftforge.fml.common.FMLLog;
57
import net.minecraftforge.fml.common.Mod;
68
import net.minecraftforge.fml.common.Mod.EventHandler;
7-
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
9+
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
10+
import net.minecraftforge.fml.common.event.FMLServerStoppedEvent;
11+
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
812
import ru.lionzxy.fastlogblock.handlers.EventHandlingManager;
13+
import ru.lionzxy.fastlogblock.ui.InfoItem;
914

10-
import java.io.File;
1115
import java.io.IOException;
1216

13-
@Mod(modid = FastLogBlock.MODID, version = FastLogBlock.VERSION)
17+
@Mod(modid = FastLogBlock.MODID, version = FastLogBlock.VERSION, acceptableRemoteVersions = "*")
1418
public class FastLogBlock {
15-
public static final File rootMinecraftPath = new File("./");
1619
public static final String MODID = "fastlogblock";
1720
public static final String VERSION = "1.0";
1821
private EventHandlingManager eventHandlingManager;
22+
private InfoItem infoitem;
1923

2024
@EventHandler
21-
public void init(final FMLInitializationEvent event) throws IOException {
25+
public void preInit(FMLPreInitializationEvent event) throws IOException {
2226
FMLLog.log.info("Initializing eventHandlingManager...");
2327
eventHandlingManager = new EventHandlingManager();
28+
this.infoitem = new InfoItem(eventHandlingManager);
2429
FMLLog.log.info("Done!");
2530
MinecraftForge.EVENT_BUS.register(eventHandlingManager);
31+
MinecraftForge.EVENT_BUS.register(this);
32+
}
33+
34+
@SubscribeEvent
35+
public void registerItems(RegistryEvent.Register<Item> event) {
36+
event.getRegistry().registerAll(infoitem);
37+
}
38+
39+
@EventHandler
40+
public void serverStopped(final FMLServerStoppedEvent event) {
41+
eventHandlingManager.stop();
2642
}
2743
}

src/main/java/ru/lionzxy/fastlogblock/handlers/EventHandlingManager.java

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,41 @@
11
package ru.lionzxy.fastlogblock.handlers;
22

3+
import net.minecraft.client.resources.I18n;
4+
import net.minecraft.entity.player.EntityPlayer;
5+
import net.minecraft.util.math.BlockPos;
6+
import net.minecraft.util.text.TextComponentTranslation;
7+
import net.minecraft.world.World;
38
import net.minecraftforge.event.world.BlockEvent;
49
import net.minecraftforge.fml.common.FMLLog;
510
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
11+
import net.minecraftforge.fml.common.gameevent.TickEvent;
12+
import ru.lionzxy.fastlogblock.io.ReadRunnable;
613
import ru.lionzxy.fastlogblock.models.BlockChangeEventModel;
714
import ru.lionzxy.fastlogblock.models.BlockChangeEventModelWithWorld;
15+
import ru.lionzxy.fastlogblock.models.FindTask;
16+
import ru.lionzxy.fastlogblock.models.FindTaskResult;
817

918
import java.io.IOException;
10-
import java.util.concurrent.Executor;
19+
import java.text.SimpleDateFormat;
20+
import java.util.Date;
21+
import java.util.UUID;
22+
import java.util.concurrent.BlockingQueue;
23+
import java.util.concurrent.ExecutorService;
1124
import java.util.concurrent.Executors;
25+
import java.util.concurrent.LinkedBlockingQueue;
1226

1327
public class EventHandlingManager {
14-
private final Executor executor = Executors.newCachedThreadPool();
28+
private final ExecutorService executor = Executors.newCachedThreadPool();
29+
private final BlockingQueue<FindTaskResult> findTaskResults = new LinkedBlockingQueue<>();
30+
private final ReadRunnable readRunnable;
1531
private final SplitterRunnable splitterRunnable;
1632

1733
public EventHandlingManager() throws IOException {
1834
this.splitterRunnable = new SplitterRunnable();
35+
this.readRunnable = splitterRunnable.getReadRunnable();
1936
splitterRunnable.runWorkers(executor);
2037
executor.execute(splitterRunnable);
38+
executor.execute(readRunnable);
2139
}
2240

2341
@SubscribeEvent
@@ -43,4 +61,62 @@ public void onBlockPlace(final BlockEvent.PlaceEvent event) {
4361
FMLLog.log.debug(blockChangeEventModel.toString());
4462
splitterRunnable.addEvent(blockChangeEventModel);
4563
}
64+
65+
@SubscribeEvent
66+
public void flushUIWait(TickEvent.ServerTickEvent event) {
67+
if (findTaskResults.isEmpty()) {
68+
return;
69+
}
70+
FindTaskResult findTaskResult;
71+
while ((findTaskResult = findTaskResults.poll()) != null) {
72+
for (BlockChangeEventModel blockEvent : findTaskResult.getBlockChangeEventModels()) {
73+
notifyAboutEvent(blockEvent, findTaskResult.getEntityPlayer());
74+
}
75+
if (findTaskResult.getBlockChangeEventModels().isEmpty()) {
76+
findTaskResult.getEntityPlayer().sendMessage(new TextComponentTranslation("message.fastlogblock:blockinfo.event.empty"));
77+
} else {
78+
findTaskResult.getEntityPlayer().sendMessage(new TextComponentTranslation("message.fastlogblock:blockinfo.event.done"));
79+
}
80+
}
81+
}
82+
83+
public void handleLogByPos(EntityPlayer entityPlayer, BlockPos blockPos, World world) {
84+
final FindTask findTask = new FindTask(blockPos, (list, player) -> {
85+
findTaskResults.add(new FindTaskResult(list, player));
86+
}, world);
87+
findTask.setEntityPlayer(entityPlayer);
88+
readRunnable.addTaskForSearch(findTask);
89+
}
90+
91+
public void stop() {
92+
executor.shutdownNow();
93+
}
94+
95+
private void notifyAboutEvent(BlockChangeEventModel blockEvent, EntityPlayer entityPlayer) {
96+
final String dateformat = I18n.format("message.fastlogblock:blockinfo.event.dateformat");
97+
final EntityPlayer playerEvent = entityPlayer.getEntityWorld().getPlayerEntityByUUID(UUID.fromString(blockEvent.getPlayernick().toString()));
98+
String nickname;
99+
if (playerEvent == null) {
100+
if (entityPlayer.getEntityWorld().getMinecraftServer().isSinglePlayer()) {
101+
nickname = entityPlayer.getDisplayNameString();
102+
} else {
103+
nickname = "Unknown(UUID: " + blockEvent.getPlayernick() + ")";
104+
}
105+
} else {
106+
nickname = playerEvent.getDisplayNameString();
107+
}
108+
final String[] args = new String[]{new SimpleDateFormat(dateformat).format(new Date(blockEvent.getTimestamp().getTime())),
109+
nickname,
110+
blockEvent.getNameblock().toString()};
111+
TextComponentTranslation textComponent;
112+
switch (blockEvent.getBlockChangeType()) {
113+
default:
114+
case INSERT:
115+
textComponent = new TextComponentTranslation("message.fastlogblock:blockinfo.event.insert", (Object[]) args);
116+
break;
117+
case REMOVE:
118+
textComponent = new TextComponentTranslation("message.fastlogblock:blockinfo.event.remove", (Object[]) args);
119+
}
120+
entityPlayer.sendMessage(textComponent);
121+
}
46122
}

src/main/java/ru/lionzxy/fastlogblock/handlers/SplitterRunnable.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package ru.lionzxy.fastlogblock.handlers;
22

3+
import net.minecraftforge.fml.common.FMLLog;
34
import ru.lionzxy.fastlogblock.config.LogConfig;
5+
import ru.lionzxy.fastlogblock.io.ReadRunnable;
46
import ru.lionzxy.fastlogblock.io.WriteRunnable;
57
import ru.lionzxy.fastlogblock.io.filesplitter.IFileSplitter;
68
import ru.lionzxy.fastlogblock.io.filesplitter.impl.BlockHashFileSplitter;
@@ -61,6 +63,8 @@ public void run() {
6163
processEvent(eventQueue.take());
6264
nickMapper.sync();
6365
blockMapper.sync();
66+
} catch (InterruptedException ie) {
67+
FMLLog.log.info("Stop SplitterRunnable");
6468
} catch (Exception e) {
6569
e.printStackTrace();
6670
}
@@ -71,6 +75,10 @@ public void addEvent(BlockChangeEventModelWithWorld blockChangeEventModelWithWor
7175
eventQueue.add(blockChangeEventModelWithWorld);
7276
}
7377

78+
public ReadRunnable getReadRunnable() {
79+
return new ReadRunnable(fileSplitter, nickMapper, blockMapper);
80+
}
81+
7482
private void processEvent(BlockChangeEventModelWithWorld event) {
7583
final File file = fileSplitter.getFileByPosAndWorld(event.getBlockPos(), event.getWorld());
7684

src/main/java/ru/lionzxy/fastlogblock/io/ReadRunnable.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ru.lionzxy.fastlogblock.io;
22

3+
import net.minecraftforge.fml.common.FMLLog;
34
import ru.lionzxy.fastlogblock.io.filesplitter.IFileSplitter;
45
import ru.lionzxy.fastlogblock.io.log.LogReader;
56
import ru.lionzxy.fastlogblock.io.mappers.BlockMapper;
@@ -35,7 +36,9 @@ public void run() {
3536
final File file = fileSplitter.getFileByPosAndWorld(findTask.getBlockPos(), findTask.getWorld());
3637
final LogReader logReader = new LogReader(file, blockMapper, nickMapper);
3738
final List<BlockChangeEventModel> blockChangeEventModels = logReader.readEventByPos(findTask.getBlockPos());
38-
findTask.getFindListener().onResultAsync(blockChangeEventModels);
39+
findTask.getFindListener().onResultAsync(blockChangeEventModels, findTask.getEntityPlayer());
40+
} catch (InterruptedException ie) {
41+
FMLLog.log.info("Stop ReadRunnable");
3942
} catch (Exception e) {
4043
e.printStackTrace();
4144
}

src/main/java/ru/lionzxy/fastlogblock/io/WriteRunnable.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ru.lionzxy.fastlogblock.io;
22

3+
import net.minecraftforge.fml.common.FMLLog;
34
import ru.lionzxy.fastlogblock.io.filesplitter.IFileSplitter;
45
import ru.lionzxy.fastlogblock.io.log.LogWritter;
56
import ru.lionzxy.fastlogblock.io.mappers.BlockMapper;
@@ -52,10 +53,19 @@ public void run() {
5253
}
5354
});
5455
withoutWork.set(true);
56+
} catch (InterruptedException ie) {
57+
FMLLog.log.info("Stop ReadRunnable");
5558
} catch (Exception e) {
5659
e.printStackTrace();
5760
}
5861
}
62+
writterMap.values().forEach(it -> {
63+
try {
64+
it.sync();
65+
} catch (IOException e) {
66+
e.printStackTrace();
67+
}
68+
});
5969
}
6070

6171
public void putEvent(BlockChangeEventModelWithWorld blockChangeEventModel) {

src/main/java/ru/lionzxy/fastlogblock/models/FindTask.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package ru.lionzxy.fastlogblock.models;
22

3+
import net.minecraft.entity.player.EntityPlayer;
34
import net.minecraft.util.math.BlockPos;
45
import net.minecraft.world.World;
56

67
public class FindTask {
78
private final BlockPos blockPos;
89
private final IFindResultListener findListener;
910
private final World world;
11+
private EntityPlayer entityPlayer;
1012

1113
public FindTask(BlockPos blockPos, IFindResultListener findListener, World world) {
1214
this.blockPos = blockPos;
@@ -25,4 +27,12 @@ public IFindResultListener getFindListener() {
2527
public World getWorld() {
2628
return world;
2729
}
30+
31+
public EntityPlayer getEntityPlayer() {
32+
return entityPlayer;
33+
}
34+
35+
public void setEntityPlayer(EntityPlayer entityPlayer) {
36+
this.entityPlayer = entityPlayer;
37+
}
2838
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11
package ru.lionzxy.fastlogblock.models;
22

3+
import net.minecraft.entity.player.EntityPlayer;
4+
5+
import java.util.List;
6+
37
public class FindTaskResult {
8+
private final List<BlockChangeEventModel> blockChangeEventModels;
9+
private final EntityPlayer entityPlayer;
10+
11+
public FindTaskResult(List<BlockChangeEventModel> blockChangeEventModels, EntityPlayer entityPlayer) {
12+
this.blockChangeEventModels = blockChangeEventModels;
13+
this.entityPlayer = entityPlayer;
14+
}
15+
16+
public EntityPlayer getEntityPlayer() {
17+
return entityPlayer;
18+
}
19+
20+
public List<BlockChangeEventModel> getBlockChangeEventModels() {
21+
22+
return blockChangeEventModels;
23+
}
424
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package ru.lionzxy.fastlogblock.models;
22

3+
import jline.internal.Nullable;
4+
import net.minecraft.entity.player.EntityPlayer;
5+
36
import java.util.List;
47

58
public interface IFindResultListener {
6-
void onResultAsync(List<BlockChangeEventModel> blockChangeEventModels);
9+
void onResultAsync(List<BlockChangeEventModel> blockChangeEventModels, @Nullable EntityPlayer entityPlayer);
710
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package ru.lionzxy.fastlogblock.ui;
2+
3+
import net.minecraft.creativetab.CreativeTabs;
4+
import net.minecraft.entity.player.EntityPlayer;
5+
import net.minecraft.item.Item;
6+
import net.minecraft.util.EnumActionResult;
7+
import net.minecraft.util.EnumFacing;
8+
import net.minecraft.util.EnumHand;
9+
import net.minecraft.util.ResourceLocation;
10+
import net.minecraft.util.math.BlockPos;
11+
import net.minecraft.util.text.TextComponentTranslation;
12+
import net.minecraft.world.World;
13+
import ru.lionzxy.fastlogblock.FastLogBlock;
14+
import ru.lionzxy.fastlogblock.handlers.EventHandlingManager;
15+
16+
import java.util.Objects;
17+
18+
public class InfoItem extends Item {
19+
private static final String ITEMNAME = "infoitem";
20+
private final EventHandlingManager eventHandlingManager;
21+
22+
public InfoItem(EventHandlingManager eventHandlingManager) {
23+
this.eventHandlingManager = eventHandlingManager;
24+
setRegistryName(FastLogBlock.MODID, ITEMNAME);
25+
final ResourceLocation registryName = Objects.requireNonNull(getRegistryName());
26+
setUnlocalizedName(registryName.toString());
27+
setCreativeTab(CreativeTabs.MISC);
28+
}
29+
30+
31+
@Override
32+
public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
33+
if (worldIn.isRemote) {
34+
return EnumActionResult.FAIL;
35+
}
36+
player.sendMessage(new TextComponentTranslation("message.fastlogblock:blockinfo.start", pos.getX(), pos.getY(), pos.getZ()));
37+
eventHandlingManager.handleLogByPos(player, pos, worldIn);
38+
return EnumActionResult.SUCCESS;
39+
}
40+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package ru.lionzxy.fastlogblock.utils;
2+
3+
import net.minecraft.entity.player.EntityPlayer;
4+
5+
public class MinecraftUtils {
6+
public static boolean isOp(EntityPlayer player) {
7+
player.getEntityWorld().getMinecraftServer();
8+
return false;
9+
}
10+
}

0 commit comments

Comments
 (0)